package com.yingsheng.bi.hks.config;

import com.binarywang.spring.starter.wxjava.pay.properties.WxPayProperties;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnProperty(prefix = "wx.pay", value = {"enabled"}, matchIfMissing = true)
@ConfigurationProperties(prefix = "wx.pay")
@Configuration
public class WxPayConfiguration {

    @Setter
    private String mqOrderNotice;

    @Setter
    private String publicKeyPath;

    @Bean
    public WxPayService wxPayService(WxPayProperties properties) {
        WxPayServiceImpl wxPayService = new WxPayServiceImpl();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(properties.getMchKey()));
        payConfig.setSubAppId(StringUtils.trimToNull(properties.getSubAppId()));
        payConfig.setSubMchId(StringUtils.trimToNull(properties.getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(properties.getKeyPath()));
        payConfig.setUseSandboxEnv(properties.isUseSandboxEnv());
        payConfig.setServiceId(StringUtils.trimToNull(properties.getServiceId()));
        payConfig.setPayScoreNotifyUrl(StringUtils.trimToNull(properties.getPayScoreNotifyUrl()));
        payConfig.setPrivateKeyPath(StringUtils.trimToNull(properties.getPrivateKeyPath()));
        payConfig.setPrivateCertPath(StringUtils.trimToNull(properties.getPrivateCertPath()));
        payConfig.setCertSerialNo(StringUtils.trimToNull(properties.getCertSerialNo()));
        payConfig.setApiV3Key(StringUtils.trimToNull(properties.getApiv3Key()));
        payConfig.setNotifyUrl(this.mqOrderNotice);
        payConfig.setPublicKeyPath(this.publicKeyPath);
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
